package com.wilmer.paperResults.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.wilmer.common.utils.DateUtils;
import com.wilmer.common.utils.SecurityUtils;
import com.wilmer.paperAnswers.domain.YwPaperStudentAnswers;
import com.wilmer.paperAnswers.service.IYwPaperStudentAnswersService;
import com.wilmer.paperResults.domain.YwPaperResultsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wilmer.paperResults.mapper.YwPaperResultsMapper;
import com.wilmer.paperResults.domain.YwPaperResults;
import com.wilmer.paperResults.service.IYwPaperResultsService;

/**
 * 考试成绩Service业务层处理
 *
 * @author zl
 * @date 2024-04-23
 */
@Service
public class YwPaperResultsServiceImpl implements IYwPaperResultsService {
    @Autowired
    private YwPaperResultsMapper ywPaperResultsMapper;
    @Autowired
    private IYwPaperStudentAnswersService ywPaperStudentAnswersService;

    @Override
    public YwPaperResults selectYwPaperResultsByResultId(Long resultId) {
        return this.ywPaperResultsMapper.selectYwPaperResultsByResultId(resultId);
    }

    @Override
    public List<YwPaperResults> selectYwPaperResultsList(YwPaperResults ywPaperResults) {
        return this.ywPaperResultsMapper.selectYwPaperResultsList(ywPaperResults);
    }

    @Override
    public List<YwPaperResults> selectPapersByUserId(YwPaperResults ywPaperResults) {
        ywPaperResults.setStudentId(SecurityUtils.getUserId());
        return this.ywPaperResultsMapper.selectPapersByUserId(ywPaperResults);
    }

    @Override
    public Integer submitTest(YwPaperResultsVO ywPaperResultsVO) {
        List<YwPaperStudentAnswers> paperStudentAnswersList = ywPaperResultsVO.getPaperStudentAnswersList();
        List<Long> idsList = paperStudentAnswersList.stream()
                .map(YwPaperStudentAnswers::getQuestionId)
                .collect(Collectors.toList());
        List<Long> duplicates = idsList.stream()
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) // 计数
                .entrySet().stream() // 创建一个新的流来处理Map的条目
                .filter(entry -> entry.getValue() > 1) // 筛选出计数大于1的条目
                .map(Map.Entry::getKey) // 提取这些条目的键（也就是原始元素）
                .collect(Collectors.toList()); // 收集到List中

        Long userId = SecurityUtils.getUserId();
        BigDecimal totalScore = BigDecimal.valueOf(0.0);
//        Long questionId = 0L;
        String a = "";
        String b = "";

        for (int i = 0; i < paperStudentAnswersList.size(); ++i) {
            if (duplicates.contains((paperStudentAnswersList.get(i)).getQuestionId())) {
                //针对多选题
                if (!a.equals((paperStudentAnswersList.get(i)).getQuestionId() + "A")) {
                    totalScore = totalScore.add((paperStudentAnswersList.get(i)).getScore());
                    paperStudentAnswersList.get(i).setIncomeScore((paperStudentAnswersList.get(i)).getScore());
                }
                a = (paperStudentAnswersList.get(i)).getQuestionId() + "A";
                if ((paperStudentAnswersList.get(i)).getIsCorrect().equals("2")) {
                    if (!b.equals((paperStudentAnswersList.get(i)).getQuestionId() + "B")) {
                        totalScore = totalScore.subtract((paperStudentAnswersList.get(i)).getScore());
                        paperStudentAnswersList.get(i).setIncomeScore(BigDecimal.valueOf(0));
                    }
                    b = (paperStudentAnswersList.get(i)).getQuestionId() + "B";
                }
                (paperStudentAnswersList.get(i)).setStudentId(userId);
                this.ywPaperStudentAnswersService.insertYwPaperStudentAnswers(paperStudentAnswersList.get(i));
            } else {
                //其他题
                if ((paperStudentAnswersList.get(i)).getIsCorrect().equals("1")) {
                    totalScore = totalScore.add((paperStudentAnswersList.get(i)).getScore());
                    paperStudentAnswersList.get(i).setIncomeScore((paperStudentAnswersList.get(i)).getScore());
                }
                (paperStudentAnswersList.get(i)).setStudentId(userId);
                this.ywPaperStudentAnswersService.insertYwPaperStudentAnswers(paperStudentAnswersList.get(i));
            }
        }

        YwPaperResults ywPaperResults = new YwPaperResults();
        ywPaperResults.setTotalScore(totalScore);
        ywPaperResults.setResultId(ywPaperResultsVO.getResultId());
        ywPaperResults.setTimeTaken(ywPaperResultsVO.getTimeTaken());
        return this.ywPaperResultsMapper.updateYwPaperResults(ywPaperResults);
    }

    @Override
    public int insertYwPaperResults(YwPaperResults ywPaperResults) {
        ywPaperResults.setCreateTime(DateUtils.getNowDate());
        return this.ywPaperResultsMapper.insertYwPaperResults(ywPaperResults);
    }

    @Override
    public int updateYwPaperResults(YwPaperResults ywPaperResults) {
        ywPaperResults.setUpdateTime(DateUtils.getNowDate());
        return this.ywPaperResultsMapper.updateYwPaperResults(ywPaperResults);
    }

    @Override
    public int deleteYwPaperResultsByResultIds(Long[] resultIds) {
        return this.ywPaperResultsMapper.deleteYwPaperResultsByResultIds(resultIds);
    }

    @Override
    public int deleteYwPaperResultsByResultId(Long resultId) {
        return this.ywPaperResultsMapper.deleteYwPaperResultsByResultId(resultId);
    }
//    /**
//     * 查询考试成绩
//     *
//     * @param resultId 考试成绩主键
//     * @return 考试成绩
//     */
//    @Override
//    public YwPaperResults selectYwPaperResultsByResultId(Long resultId)
//    {
//        return ywPaperResultsMapper.selectYwPaperResultsByResultId(resultId);
//    }
//
//    /**
//     * 查询考试成绩列表
//     *
//     * @param ywPaperResults 考试成绩
//     * @return 考试成绩
//     */
//    @Override
//    public List<YwPaperResults> selectYwPaperResultsList(YwPaperResults ywPaperResults)
//    {
//        return ywPaperResultsMapper.selectYwPaperResultsList(ywPaperResults);
//    }
//
//    /**
//     * 新增考试成绩
//     *
//     * @param ywPaperResults 考试成绩
//     * @return 结果
//     */
//    @Override
//    public int insertYwPaperResults(YwPaperResults ywPaperResults)
//    {
//        ywPaperResults.setCreateTime(DateUtils.getNowDate());
//        return ywPaperResultsMapper.insertYwPaperResults(ywPaperResults);
//    }
//
//    /**
//     * 修改考试成绩
//     *
//     * @param ywPaperResults 考试成绩
//     * @return 结果
//     */
//    @Override
//    public int updateYwPaperResults(YwPaperResults ywPaperResults)
//    {
//        ywPaperResults.setUpdateTime(DateUtils.getNowDate());
//        return ywPaperResultsMapper.updateYwPaperResults(ywPaperResults);
//    }
//
//    /**
//     * 批量删除考试成绩
//     *
//     * @param resultIds 需要删除的考试成绩主键
//     * @return 结果
//     */
//    @Override
//    public int deleteYwPaperResultsByResultIds(Long[] resultIds)
//    {
//        return ywPaperResultsMapper.deleteYwPaperResultsByResultIds(resultIds);
//    }
//
//    /**
//     * 删除考试成绩信息
//     *
//     * @param resultId 考试成绩主键
//     * @return 结果
//     */
//    @Override
//    public int deleteYwPaperResultsByResultId(Long resultId)
//    {
//        return ywPaperResultsMapper.deleteYwPaperResultsByResultId(resultId);
//    }
}
